package com.ruoyi.project.dzbqUtil.util;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class SocketSendUtil {
    public static boolean sendMsg(String host, int port, String msg) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            socket = new Socket(host, port);
            outputStream = socket.getOutputStream();
            byte[] bytes = SocketUtil.hexStringToByteArray(msg);
            outputStream.write(bytes);
            outputStream.flush();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (socket.isConnected()){
                    return true;
                }else {
                    return false;
                }
            } catch (Exception e) {
//                throw new RuntimeException(e);
                System.out.println(e.getMessage());
                return false;
            }

        }
    }
}
